from sqlalchemy import Column, BigInteger, String, Integer, DateTime, Index, PrimaryKeyConstraint, ForeignKey
from sqlalchemy.orm import relationship

from config.database import Base


class BgAffiliateRelation(Base):
    """推广关系表"""
    __tablename__ = 'bg_affiliate_relation'

    inviter_id = Column(BigInteger,  ForeignKey('sys_user.user_id'),  nullable=False, comment='推广者用户ID', index=True)
    invitee_id = Column(BigInteger,  ForeignKey('sys_user.user_id'), nullable=False, unique=True, comment='被推广者用户ID')
    affiliate_code = Column(String(32), nullable=False, comment='推广码', index=True)
    invite_time = Column(DateTime, nullable=False, comment='邀请时间')
    status = Column(Integer, nullable=False, default=1, comment='状态(0:无效 1:有效)')

    # 关联关系
    inviter = relationship("SysUser", foreign_keys=[inviter_id], back_populates="invite_relations")
    invitee = relationship("SysUser", foreign_keys=[invitee_id], back_populates="invited_relation")

    __table_args__ = (
        PrimaryKeyConstraint("inviter_id", "invitee_id"),
    )
